const path = require('path')
const webpack = require('webpack')
const packageJson = require('./package.json')
const buildDate = JSON.stringify(new Date().toLocaleString())
const createThemeColorReplacerPlugin = require('./config/plugin.config')

function resolve (dir) {
    return path.join(__dirname, dir)
}

// eslint-disable-next-line no-unused-vars
const isProd = process.env.NODE_ENV === 'production'
// eslint-disable-next-line no-unused-vars
const assetsCDN = {
    // webpack build externals
    externals: {
        vue: 'Vue',
        'vue-router': 'VueRouter',
        vuex: 'Vuex',
        axios: 'axios'
    },
    css: [],
    // https://unpkg.com/browse/vue@2.6.10/
    js: [
        '//cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js',
        '//cdn.jsdelivr.net/npm/vue-router@3.5.1/dist/vue-router.min.js',
        '//cdn.jsdelivr.net/npm/vuex@3.1.1/dist/vuex.min.js',
        '//cdn.jsdelivr.net/npm/axios@0.21.1/dist/axios.min.js'
    ]
}

// vue.config.js
const vueConfig = {
    pluginOptions: {
        windicss: {
            // see https://github.com/windicss/vite-plugin-windicss/blob/main/packages/plugin-utils/src/options.ts
            // preflight: false,
        },
    },
    configureWebpack: {
        // webpack plugins
        plugins: [
            // Ignore all locale files of moment.js
            new webpack.IgnorePlugin({
                contextRegExp: /^\.\/locale$/,
                resourceRegExp: /moment$/
            }),
            new webpack.DefinePlugin({
                APP_VERSION: `"${packageJson.version}"`,
                BUILD_DATE: buildDate
            }),
            new webpack.ProvidePlugin({
                process: 'process/browser',
                Buffer: ['buffer', 'Buffer']
            })
        ],
        resolve: {
            // 1.不需要node polyfilss
            alias: {
                crypto: require.resolve("crypto-browserify"),
                stream: require.resolve("stream-browserify")
            }
        },
        // en_US: `if prod, add externals`
        // zh_CN: `这里是用来控制编译忽略外部依赖的，与 config.plugin('html') 配合可以编译时引入外部CDN文件依赖`
        // externals: isProd ? assetsCDN.externals : {}
    },

    chainWebpack: config => {
        config.resolve.alias.set('@$', resolve('src'))

        // const svgRule = config.module.rule('svg')
        // svgRule.uses.clear()
        // svgRule
        //   .oneOf('inline')
        //   .resourceQuery(/inline/)
        //   .use('vue-svg-icon-loader')
        //   .loader('vue-svg-icon-loader')
        //   .end()
        //   .end()
        //   .oneOf('external')
        //   .use('file-loader')
        //   .loader('file-loader')
        //   .options({
        //     name: 'assets/[name].[hash:8].[ext]',
        //     esModule: false
        //   })

        // en_US: If prod is on assets require on cdn
        // zh_CN: 如果是 prod 模式，则引入 CDN 依赖文件，有需要减少包大小请自行解除依赖
        //
        // if (isProd) {
        //   config.plugin('html').tap(args => {
        //     args[0].cdn = assetsCDN
        //     return args
        //   })
        // }
    },

    css: {
        loaderOptions: {
            less: {
                modifyVars: {
                    // less vars，customize ant design theme
                    // 定制主题色
                    'primary-color': '#5077a5',
                    'link-color': '#5077a5',
                    'border-radius-base': '4px'
                },
                // DO NOT REMOVE THIS LINE 
                javascriptEnabled: true
            }
        }
    },

    devServer: {
        // host: 'admin.aidolphins.com',
        host: 'localhost',
        // host: '192.168.0.4',
        // development server port 8000
        port: 9528,
        // port: 11000,
        open: true,
        // If you want to turn on the proxy, please remove the mockjs /src/main.jsL11
        proxy: {

            [process.env.VUE_APP_BASE_API]: {
                // target: `http://119.188.241.207:11000`, //测试本地接口
                // target: `http://192.168.0.118:11000/`,
                // target: `http://192.168.0.2:8080`,
                // target: `http://192.168.8.52:8080`,
                target: `http://119.188.246.51:11000`,
                //  target: `https://dgp-test.bmw.com.cn/prod-api`,
                // target: `http://192.168.0.103:80 80/`,
                // target: `http://192.168.0.106:8080/`,
                // target: `http://192.168.0.103:8080/`,
                // target: `https://mingdao.btenant.com/`,
                // target: `http://192.168.0.105:8080/`,
                // target: `http://192.168.0.107:8080/`,
                // target: `http://www.aidolphins.com:11000`,
                changeOrigin: true,
                ws: false,
                secure: false,
                pathRewrite: {
                    ["^" + process.env.VUE_APP_BASE_API]: ""
                }
            },
            /**
             * 配置文件服务器代理
             */
            '/api': {
                    // target: 'https://dgp-test-oss.oss-cn-beijing.aliyuncs.com', //填写请求的目标地址-测试环境
                target: 'https://dgp-pro-bucket.oss-cn-beijing-internal.aliyuncs.com', //填写请求的目标地址-生产环境
                changOrigin: true, //允许跨域
                pathRewrite: {
                '^/api': '' //请求的时候使用这个api就可以
            }
            }

        }
    },

    // disable source map in production
    productionSourceMap: false,
    lintOnSave: false,
    // babel-loader no-ignore node_modules/*
    transpileDependencies: []
}

// preview.pro.loacg.com only do not use in your production;
if (process.env.VUE_APP_PREVIEW === 'true') {
    // add `ThemeColorReplacer` plugin to webpack plugins
    vueConfig.configureWebpack.plugins.push(createThemeColorReplacerPlugin())
}

module.exports = vueConfig
